////////////////////////////////////////////////////////////////////////////////
/// Midnight Circus 2006
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// As described in "Documentation\nwn2toolsethelp.html"
//
// Containers are placeables that have inventory and can be used by a player to
// access the items and storage space. To set a placeable as a container, select
// the placeable and look at its properties window.
//
// In the Properties tab, make sure that the following fields are set as follows:
//    Has Inventory?     =        True
//    Inventory Size     >        1, though 136 is average for a chest
//    Static             =        False
//    Usable?            =        True
//
// To change any of these properties, just click on the field next to the name.
// These fields set the placeable as being capable of holding items and usable
// by creatures. A player can now use this object in the game and take items
// from this placeable (as well as store them). If this placeable is destroyed,
// it will drop any items it held.
////////////////////////////////////////////////////////////////////////////////

namespace MidnightCircus
{
    public class MakeContainer : NWN2Toolset.Plugins.INWN2Plugin
    {
        private TD.SandBar.MenuButtonItem m_cMenuItem;

        private void CreateContainer(short inventory_size)
        {
            System.Windows.Forms.ContainerControl container = NWN2Toolset.NWN2ToolsetMainForm.App.ActiveControl as System.Windows.Forms.ContainerControl;

            if ((container != null) && (container.ActiveControl != null))
            {
                NWN2Toolset.NWN2.Views.NWN2AreaViewer viewer = container.ActiveControl as NWN2Toolset.NWN2.Views.NWN2AreaViewer;

                foreach (NWN2Toolset.NWN2.Data.Instances.INWN2Instance instance in viewer.SelectedInstances)
                {
                    if (instance.GetType() == typeof(NWN2Toolset.NWN2.Data.Instances.NWN2PlaceableInstance))
                    {
                        NWN2Toolset.NWN2.Data.Instances.NWN2PlaceableInstance placeable = instance as NWN2Toolset.NWN2.Data.Instances.NWN2PlaceableInstance;
                        placeable.HasInventory = true;
                        placeable.InventorySize = inventory_size;
                        placeable.Static = false;
                        placeable.Usable = true;

                        string result = System.String.Format( "{0} was successfully turned into a container!", placeable.Name );
                        System.Windows.Forms.MessageBox.Show(result);
                    }
                }
            }
        }

        private void HandlePluginLaunch(object sender, System.EventArgs e)
        {
            // This code is executed when the menu is selected
            CreateContainer(136);
        }

        ////////////////////////////////////////////////////////////////////////
        // called when the toolset is started
        ////////////////////////////////////////////////////////////////////////
        public void Load(NWN2Toolset.Plugins.INWN2PluginHost cHost)
        {
        }

        ////////////////////////////////////////////////////////////////////////
        // called when the toolset is closed
        ////////////////////////////////////////////////////////////////////////
        public void Unload(NWN2Toolset.Plugins.INWN2PluginHost cHost)
        {
        }

        ////////////////////////////////////////////////////////////////////////
        // called when the plugin is selected
        ////////////////////////////////////////////////////////////////////////
        public void Startup(NWN2Toolset.Plugins.INWN2PluginHost cHost)
        {
            m_cMenuItem = cHost.GetMenuForPlugin(this);
            m_cMenuItem.Activate += new System.EventHandler(this.HandlePluginLaunch);
        }

        ////////////////////////////////////////////////////////////////////////
        // called when the plugin is terminated
        ////////////////////////////////////////////////////////////////////////
        public void Shutdown(NWN2Toolset.Plugins.INWN2PluginHost cHost)
        {
        }

        public TD.SandBar.MenuButtonItem PluginMenuItem
        {
            get
            {
                return m_cMenuItem;
            }
        }

        // Properties
        public string DisplayName
        {
            get
            {
                return "Make Container";
            }
        }

        public string MenuName
        {
            get
            {
                return "Make Container";
            }
        }

        public string Name
        {
            get
            {
                return "Make Container";
            }
        }

        public object Preferences
        {
            get
            {
                return null;
            }
            set
            {
            }
        }
    }
}
